#!/bin/sh
#抽取明细信息到dws_thousand_kilomoter_sd表中
CUR_DIR=$(cd "$(dirname "${BASH_SOURCE-$0}")"; pwd)
UP_DIR=$(dirname $(dirname $(dirname "$PWD")))
echo ${UP_DIR}
source ${UP_DIR}/system_env.sh


START_TIME=$(date +%s)
hive_db=${hive_db}

if [ -z "${hive_db}" ];then
  hive_db=dataplatform_trip
else
  hive_db=${hive_db}
fi


if [ -z "$1" ];then
  part_time=$(date -d "1 days ago" +%Y%m%d)
else
  part_time=$1
fi

sql_suffix=$(cat <<EOF
  insert overwrite table ${hive_db}.dws_thousand_kilometer_sd
select key,uid,tid,mileagesection,sectionstartfuel,
       sectionendfuel,sectionfuel,sectionstartmileage,
	   sectionendmileage,sectionmileage,sectionurea,d.strains_id,d.strains,e.driver_type_id,e.driver_type,f.horse_power_id,f.horse_power,g.bridge_speed_ratio_id,g.bridge_speed_ratio,h.emission_standards_id,h.emission_standards
	   from ${hive_db}.dwd_trip_thousand_kilometer_sd a
	  join ${hive_db}.ods_business_car c
       on (a.tid = c.auto_terminal)
	 join ${hive_db}.ods_business_car_type b
	   on (b.car_model=c.car_model_code)
	 join ${hive_db}.dim_strains d
	  on (b.strains = d.strains)
	 join ${hive_db}.dim_driver_type e
	  on (b.driver_type = e.driver_type)
	 join ${hive_db}.dim_horse_power f
	  on (b.horse_power = f.horse_power)
	 join ${hive_db}.dim_bridge_speed_ratio g
	  on (b.bridge_speed_ratio = g.bridge_speed_ratio)
	 join ${hive_db}.dim_emission_standards h
	 on (b.emission_standards = h.emission_standards)
	  where c.auto_terminal != '' and c.auto_terminal is not null;

EOF
)

CUR_DIR=$(cd "$(dirname "$0")"; pwd)
hive_config=$(cat  "${CUR_DIR}"/hive.config)
sql="${hive_config}${sql_suffix}"
echo "导入dws_thousand_kilomoter_sd的SQL为：${sql}"

#执行语句·
hive -e "${sql}"
if [ $? -ne 0 ];then
  echo "导入dws_thousand_kilomoter_sd失败，耗时$((`date +%s`-START_TIME))秒..."
else
  echo "导入dws_thousand_kilomoter_sd成功，耗时$((`date +%s`-START_TIME))秒..."
fi